local huaiju = fk.CreateSkill{
  name = "doublez__huaiju"
}

Fk:loadTranslationTable{
  ["doublez__huaiju"] = "怀橘",
  [":doublez__huaiju"] = "每轮限一次，每回合结束时，你可以从本回合弃牌堆获得一张<font color='red'>♥</font>基本牌或普通锦囊牌并明置之。"
    .."你本轮失去此牌后，你展示手牌并令一名角色将手牌数摸至X张（X为4-你手牌中的花色数）。",
  
  ["#doublez__huaiju_draw"] = "怀橘：令一名角色摸至 %arg 张牌",
  ["@@doublez__huaiju-round"] = "怀橘",

  ["$doublez__huaiju1"] = "情深舐犊，怀擢藏橘。",
  ["$doublez__huaiju2"] = "袖中怀绿桔，遗母报乳哺。",
}

local DIY = require "packages/diy_utility/diy_utility"

huaiju:addEffect(fk.TurnEnd, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and table.find(player:getTableMark("doublez__huaiju-turn"), function (c) return player.room:getCardArea(c) == Card.DiscardPile end) and player:usedSkillTimes(huaiju.name, Player.HistoryRound) < 1
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if not room:askToSkillInvoke(player, {skill_name = huaiju.name}) then return end
    local choices = table.simpleClone(player:getTableMark("doublez__huaiju-turn"))
    choices = table.filter(player:getTableMark("doublez__huaiju-turn"), function (c) return player.room:getCardArea(c) == Card.DiscardPile end)
    local cards, choice = room:askToChooseCardsAndChoice(player, {
      cards = choices,
      skill_name = huaiju.name
    })
    if choice == "Cancel" then return end
    event:setCostData(self, cards[1])
    return true
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local card = Fk:getCardById(event:getCostData(self))
    room:obtainCard(player, card, true, fk.ReasonPrey, player, huaiju.name, "@@doublez__huaiju-round")
    DIY.showCards(player, card)
  end
})

huaiju:addEffect(fk.AfterCardsMove, {
  is_delay_effect = true,
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(self, true, true) then return end
    for _, move in ipairs(data) do
      if move.from and move.from == player and ((move.to ~= player) or (move.toArea ~= Player.Hand and move.toArea ~= Player.Equip))then
        for _, info in ipairs(move.moveInfo) do
          if (info.fromArea == Player.Hand or info.fromArea == Player.Equip) 
             and Fk:getCardById(info.cardId):getMark("@@doublez__huaiju-round") > 0 then
            return true
          end
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local draw_num
    if player:getHandcardNum() > 0 then
      player:showCards(player:getCardIds(Player.Hand))
      
      local suits = {}
      for _, cid in ipairs(player:getCardIds(Player.Hand)) do
        table.insertIfNeed(suits, Fk:getCardById(cid).suit)
      end
      draw_num = 4 - #suits
    else
      draw_num = 4
    end
    local targets = table.filter(room:getAlivePlayers(), function (player)
      return player:getHandcardNum() < draw_num
    end)

    if #targets > 0 then
      local to = room:askToChoosePlayers(player, {
        min_num = 1,
        max_num = 1,
        skill_name = huaiju.name,
        targets = targets,
        prompt = "#doublez__huaiju_draw:::"..draw_num,
        cancelable = false
      })
      room:drawCards(to[1], draw_num - to[1]:getHandcardNum(), huaiju.name)
    end
  end
})

huaiju:addEffect(fk.AfterCardsMove, {
  can_refresh = function (self, event, target, player, data)
    if not player:hasSkill(self, true, true) then return end
    return true
  end,
  on_refresh = function (self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          local c = Fk:getCardById(info.cardId)
          if c.suit == Card.Heart and (c:isCommonTrick() or c.type == Card.TypeBasic) then
            player.room:addTableMarkIfNeed(player, "doublez__huaiju-turn", info.cardId)
          end
        end
      end
    end
  end
})

huaiju:addAcquireEffect(function (self, player, is_start)
  if not is_start then
    local room = player.room
    local cards = {}
    room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function (e)
      for _, move in ipairs(e.data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            local c = Fk:getCardById(info.cardId)
            if c.suit == Card.Heart and (c:isCommonTrick() or c.type == Card.TypeBasic) then
              player.room:addTableMarkIfNeed(player, "doublez__huaiju-turn", info.cardId)
            end
          end
        end
      end
    end, Player.HistoryTurn)
    if #cards > 0 then
      room:setPlayerMark(player, "doublez__huaiju-turn", cards)
    end
  end
end)

return huaiju